const http = require('http');
// console.log(http);

const { render } = require("../tool/read");

// 创建本地服务器来从其接收数据
var server = http.createServer(async function (req, res) { // 接收到请求时执行的回调函数
    // req  => request 
    // res  => response
    // console.log(111111);

    if (req.url != "/favicon.ico") {  //排除网页标题图标
        console.log("接收到请求!!!");

        var url = req.url;
        console.log(url);

        if (url == "/") {
            var result = await render("./index.html");
            res.write(result);
        } else {
            try {
                var result = await render(url);
                res.write(result);
            } catch (err) {
                var result = await render("./error/404.html");
                res.write(result);
            }
        }


    }

    res.end();

})
// console.log(server);

server.listen(3000, function () { // 服务启动时执行的回调函数
    console.log("服务已启动:localhost:3000");
})